package com.security.door.controller.deviceManage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.devFuncpoint;
import com.security.door.model.dto.devFuncpointDto;
import com.security.door.service.IdevFuncPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
@Api(value = "WEB - devFunctionPointController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class devFunctionPointController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IdevFuncPointService idevFuncPointService;


    @GetMapping("/funcPoint/queryListWithPage")
    @ApiOperation(value = "根据设备类型查询设备功能点", notes = "根据设备类型查询设备功能点")
    public Wrapper<PageInfo<devFuncpoint>> listAll() {
        String param = httpServletRequest.getParameter("param");
        JSONObject _json = new JSONObject();
        devFuncpointDto entityDto = (devFuncpointDto) _json.toJavaObject(JSON.parseObject(param), devFuncpointDto.class);
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = idevFuncPointService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping("/funcPoint/add")
    @ApiOperation(value = "新增功能点", notes = "新增功能点")
    public Wrapper InsertDevicefuncInfo(@RequestBody devFuncpoint entity) {
        if (entity.getGuid() == null) {
            if (idevFuncPointService.hasActiveNo(entity))
                return WrapMapper.error("功能序号已经存在");
            entity.setGuid(UUID.randomUUID().toString());
            idevFuncPointService.save(entity);
        } else {
            idevFuncPointService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping("/funcPoint/del/{guids}")
    @ApiOperation(value = "删除功能点", notes = "删除功能点")
    public Wrapper DeleteDevicefuncInfo(@PathVariable("guids") String guids) {
        idevFuncPointService.deleteByGuids(guids);
        return WrapMapper.ok("操作成功");
    }
}
